package tool

import (
	"asocrawler/conf"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
)

//保存文件
func SaveFile(body []byte, fileType, appId string) (filePath string, err error) {
	var fileName = appId + "." + fileType
	err = os.MkdirAll("./file/img", 0777)
	if err != nil {
		return
	}

	err = ioutil.WriteFile("./file/img/"+fileName, body, 0755)
	if err != nil {
		return
	}

	filePath = conf.Conf.Cdn + "/file/img/" + fileName

	return
}

//调用脚本同步到cdn
func SyncToCdn() (err error) {
	c := exec.Command("./rsync.sh")
	stdout, err := c.StdoutPipe()
	c.Stderr = c.Stdout
	err = c.Start()
	if err != nil {
		// todo 报警
		log.Println("SyncToCdn.Start()失败 err=", err.Error())
		return err
	}
	log.Println("SyncToCdn stdout:", stdout)
	err = c.Wait()
	if err != nil {
		return
	}
	return
}
